<!--
 * @Author: Summer
 * @LastEditors: Summer
 * @Description: 
 * @Date: 2021-11-05 11:26:27 +0800
 * @LastEditTime: 2021-11-09 12:32:30 +0800
 * @FilePath: \pj-node-imserver-ballroom\static\index.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    测试页面
</body>
<script>
    Date.prototype.format = function (fmt) {
        let format = {
            y: String(this.getFullYear()),
            M: String(this.getMonth() + 1).padStart(2, "0"),
            d: String(this.getDate()).padStart(2, "0"),
            H: String(this.getHours()).padStart(2, "0"),
            m: String(this.getMinutes()).padStart(2, "0"),
            s: String(this.getSeconds()).padStart(2, "0"),
            S: String(this.getMilliseconds()).padStart(3, "0"),
        }
        return fmt.replace(/[yMdHmsS]/g, function (key) {
            return format[key] || key;
        });
    }
    var ws = new WebSocket("ws://10.9.16.12:8080");
    ws.onopen = function () { console.log("连接成功"); ws.send("0") }
    ws.onclose = function (code, msg) { console.log("连接断开", code, msg) }
    ws.onerror = function (error) { console.log("连接异常", error) }
    ws.onmessage = function ({ data }) {
        let str = event.data;
        let cmd = +str.slice(0, 1); // 一个单位符号作为指令操作码，
        let msg = str.slice(1);
        switch (cmd) {
            case 1: {
                setTimeout(() => ws.send(0), 1000)
                // console.log("SystemTime", new Date(+msg).format("y/M/d-H:m:s:S"))
                break;
            }
            case 2: {
                let { path, data } = JSON.parse(msg)
                console.log(path, ":", data);
                break;
            }
            default: {

                break;
            }
        }
    }
    function send(path, data){
        this.ws.send("2" + JSON.stringify({path, data}))// 2["user.login",{"user":{ "token":"12347" }}]
    }
</script>

</html>